<template>
  <div class="quota-list-content">
    <van-tabs class="quota-swiper-tab" :swipe-threshold="3" :ellipsis="false">
      <van-tab v-for="(quota,cate) in cgjybzQuota" :key="cate" :title="quota.division">
        <div class="content-box">
          <!-- <p class="font">{{quota.division}}</p> -->
          <div class="index-list-wrap" v-for="(child,ids) in childCate(quota.list)" :key="ids">
            <p class="sub-title">{{child.industryName}}</p>
            <div class="child-item-wrap">
              <div class="item-wrap"  v-for="(item,index) in child.list" :key="index" :class="computedNum(index,item,child.list) == 1?'full-item':''">
                <IndexItem :item="item" :index="index" :num="computedNum(index,item,child.list)" @activeItemChange="activeItemChange"></IndexItem>

                <IndexDetail
                  v-if="item.isShow"
                  class="index-content"
                  :index="computedNum(index,item,child.list) == 1?0:index"
                  :detail="item.detail"
                  :quota="item"
                  :threshold="item.threshold"
                  @Favrefresh="Favrefresh($event, item)"></IndexDetail>
              </div>
            </div>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, watch } from 'vue'
import { useRouter } from 'vue-router'
import HomeServe from '@/api/home'
import IndexItem from '@/views/home/components/IndexItem.vue'
import IndexDetail from '@/views/home/components/IndexDetail.vue'

defineOptions({
  name: 'QuotaIndexList'
})

const props = defineProps({
  cgjybzIndustry: {
    type: Array,
    default: []
  },
})

const emit = defineEmits([])

const cgjybzQuota = ref([])
watch(
  () => props.cgjybzIndustry,
  (val) => {
    setTimeout(() => {
      cgjybzQuota.value = val
    })
  }
)

const activeIndex = ref(null)
const activeItemChange = async (item: any,index: number) =>{
  item.id = item.quotaId || item.id
  if (!item.isShow) {
    if(item.industryName == '国家重点管控资源'){
      item.detail = {
        predictEntity: item.predictEntity,
      }
    }else{
      const resp = await HomeServe.getRightDetailsByTypeId({
        typeId: item.quotaId
      })
      item.detail = resp.data
    }
  } else {
    item.detail = null
  }
  
  if(activeIndex.value != index){
    cgjybzQuota.value.forEach(ele=>{
      ele.list.forEach(ele1=>{
        ele1.isShow = false;
      })
    })
  }
  item.isShow = !item.isShow;
  activeIndex.value = index;
}

const childCate = (list: any) => {
  let newArr = [];
  list.forEach(item => {
    const parent = newArr.find(c => c.industryName == item.industryName)
    if (parent) {
      parent.list.push(item)
    } else {
      const obj = {
        industryName: item.industryName,
        list: [item]
      }
      newArr.push(obj)
    }
  })
  return newArr
}

const Favrefresh = (ev: any, item: any) => {
  item.collectFlag = ev
}

const computedNum = (index: number,item: any, list: any) => {
  if(item.industryName == '国家重点管控资源'){
    return 1
  }
  let len = list.length;
  //计算列表是否是单数，如果是单数则最后一条独自占据一行
  if((len%2!=0) && ((index+1)==len)){
    return 1
  }else{
    return 2
  }
}

</script>

<style lang="less" scoped>
.quota-list-content {
  .font {
    margin: 17px 0 10px;
    text-align: center;
    font-weight: bold;
  }
  .content-box:first-child{
    .font{
      margin-top: 3px;
    }
  }
  .index-list-wrap {
    background: rgba(255, 255, 255, 0.6);
    border-radius: 10px 7.69px;
    border: 2px solid #fff;
    padding: 3px 8px 4px;
    margin: 0 12px 16px;
    .sub-title{
      margin-left: 3px;
    }
    .child-item-wrap{
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
    }
    .item-wrap{
      width: 159px;
      margin-bottom: 10px;
      &:nth-child(even){
        .index-content{
          left: -170px;
        }
      }
      &.full-item{
        width: 100%;
        &:nth-child(even){
          .index-content{
            left: 0px;
          }
        }
      }
    }
  }
  .quota-swiper-tab{
    :deep(.van-tabs__wrap){
      height: 26px;
      position: fixed;
      top: 135px;
      .van-tabs__nav{
        background: transparent;
        .van-tab{
          border-radius: 13px;
          background: rgba(51, 132, 255, 0.2);
          margin: 0 5px 0 3px;
          font-weight: 400;
          font-size: 13px;
          color: #212121;
        }
        .van-tab--active{
          background: #3D76CC;
          color: #fff;
        }
      }
      .van-tabs__line{
        background: transparent;
      }
    }
  }
}
</style>
